﻿#region Using directives

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Weazel.Toad.Application.Items;

#endregion

namespace Weazel.Toad.Application.Actions
{
  public class OpenFileAction : ActionBase
  {
    public OpenFileAction()
    {

    }

    public override void Execute(object sender, EventArgs e)
    {
      OpenFileDialog openDialog = new OpenFileDialog();
      openDialog.CheckFileExists = true;
      openDialog.Multiselect = false;
      openDialog.Filter = 
        "Supported Files(*.fdl;*.vhd;*.txt)|*.fdl;*.vhd;*.txt| " + 
        "Gezel Files (*.fdl)|*.fdl| " + 
        "Vhdl Files (*.vhd)|*.vhd| " +
        "All Files (*.*)|*.*";

      if (openDialog.ShowDialog() == DialogResult.OK)
      {
        string filename = openDialog.FileName;
        FileInfo fi = new FileInfo(filename);
        string displayName = fi.Name;

        switch (fi.Extension.ToLower())
        {
          case ".vhd":
            VhdlTextFile vhdlTextFile = new VhdlTextFile();
            vhdlTextFile.Filename = filename;
            vhdlTextFile.DisplayName = displayName;
            vhdlTextFile.Open();
            Application.Instance.AddOpenItem(vhdlTextFile);
            break;
          case ".fdl":
            GezelTextFile gezelTextFile = new GezelTextFile();
            gezelTextFile.Filename = filename;
            gezelTextFile.DisplayName = displayName;            
            gezelTextFile.Open();
            Application.Instance.AddOpenItem(gezelTextFile);
            break;
          default:
            TextFile textFile = new TextFile();
            textFile.Filename = filename;
            textFile.DisplayName = displayName;
            textFile.Open();
            Application.Instance.AddOpenItem(textFile);
            break;
        }
      }
    }
  }
}
